/*
 * @(#)RProp.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model.algorithm;

import xfuzzy.xfsl.model.*;
import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;

/**
 * Algoritmo Resilient Propagation
 * 
 * @author Francisco Jos� Moreno Velo
 *
 * @see "Riedmiller, M., Braun, H., RPROP: A fast and robust backpropagation 
 * learning strategy, in Proc. 4th Australian Conference on Neural Networks, 
 * pp. 169-72, Sydney, 1993."
 */
public class RProp extends XfslGradientBasedAlgorithm {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Valor inicial de actualizaci�n de los par�metros
	 */
	private double update;
	
	/**
	 * Factor de incremento
	 */
	private double increase;
	
	/**
	 * Factor de decremento
	 */
	private double decrease;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public RProp() {
		this.update = 0.1;
		this.increase = 1.5;
		this.decrease = 0.7;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el c�digo de identificaci�n del algoritmo
	 */
	public int getCode() {
		return RPROP;
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	public void setParameters(double[] param) throws XflException {
		if(param.length != 3) throw new XflException(26);
		update = test(param[0], POSITIVE);
		increase = test(param[1], INCREASE);
		decrease = test(param[2], DECREASE);
	}

	/**
	 * Obtiene los par�metros de configuraci�n del algoritmo
	 */
	public XfslAlgorithmParam[] getParams() {
		XfslAlgorithmParam[] pp = new XfslAlgorithmParam[3];
		pp[0] = new XfslAlgorithmParam(update, 0.1, POSITIVE, "Initial Update");
		pp[1] = new XfslAlgorithmParam(increase, 1.5, INCREASE, "Increase Factor");
		pp[2] = new XfslAlgorithmParam(decrease, 0.7, DECREASE, "Decrease Factor");
		return pp;
	}

	/**
	 * Ejecuta una iteraci�n del algoritmo
	 */
	public XfslEvaluation iteration(Specification spec, XfdsDataSet pattern,
			XfslErrorFunction ef) throws XflException {
		XfslEvaluation eval = null;
		Parameter[] param = spec.getAdjustable();

		if(init) {
			for(int i=0; i<param.length; i++) param[i].setMisc(update);
			init = false;
		}

		XfslEvaluation prev = computeErrorGradient(spec,pattern,ef);

		boolean backtracking = true;
		int counter = 10;
		while(backtracking && counter >0) {
			double[] delta = new double[param.length];
			for(int i=0; i<param.length; i++) {
				double signo = param[i].getPrevDeriv() * param[i].getDeriv();
				if( signo >0 ) delta[i] = param[i].getMisc()*increase;
				else if( signo <0 ) delta[i] = param[i].getMisc()*decrease;
				else delta[i] = param[i].getMisc();
				if(param[i].getDeriv()>0) param[i].setDesp(-delta[i]);
				else if(param[i].getDeriv()<0) param[i].setDesp(delta[i]);
				else param[i].setDesp(0);
			}
			spec.update();

			eval = ef.evaluate(spec,pattern,prev.error);
			if(eval.error > prev.error) for(int i=0; i<param.length; i++) {
				param[i].value -= param[i].getPrevDesp();
				param[i].setMisc(param[i].getMisc()*decrease);
			} else {
				backtracking = false;
				for(int i=0; i<param.length; i++) param[i].setMisc(delta[i]);
			}
			counter--;
		}

		for(int i=0; i<param.length; i++) param[i].forward();
		return eval;
	}
}
